'''
Created on 01/ott/2009

@author: syn
'''

from ui import get_glade_file, get_save_dialog
import gtk

class GladeBase(object):
    def __init__(self, component, top_component=None):
        self.builder = gtk.Builder()
        self.builder.add_from_file(get_glade_file(component))
        self.root_component = self.builder.get_object(component)
        
        # Top level component defaults to root_component for the glade file  
        if top_component:
            self.top_component = top_component
        else:
            self.top_component = self.root_component
        
        self._retrieve_components()
        self.builder.connect_signals(self)
    
    def _retrieve_components(self):
        pass
    
    def get_from_builder(self, object_name, attr_name=None):
        object = self.builder.get_object(object_name)
        if not object:
            logger.debug("glade file has no object %s" % object_name)
            return
        
        if attr_name:
            attribute = attr_name
        else:
            attribute = object_name
        
        if not hasattr(self, attribute):
            setattr(self, attribute, object)
            
    def get_top_component(self):
        return self.top_component
    
    # Generic signals callbacks
    def main_component_show(self):
        self.root_component.show()
           
    def destroy(self):
        self.root_component.destroy()
    
    def gtk_main_quit(self, object, data=None):
        gtk.main_quit()
    
    
